################################################################################
# Function to merge continuation values with observations

# First load required packages
source("loadPackages.R")
loadPackages(c("dplyr", "purrr"))

# Then source cvFunctions.R
source("cvFunctions.R")


eqMerge <- function(treatment.names.full, round.vals, experimentalResults){
  
  eq.df.full = expand.grid(treatment.names.full, round.vals) %>% 
    dplyr::rename(treatment2 = Var1, roundId = Var2) %>%
    arrange(treatment2, roundId) %>%
    dplyr::mutate(mover = ifelse((roundId + 1)%%2 == 1, "Player1", "Player2"))%>% 
    mutate(across(where(is.factor), ~ as.character(.))) %>%
    as_tibble()
  
  
  # Create dataframe with equilibrium values using CV function
  
  # Since we subset the endogenous treatments to cases where both individuals choose 100
  # we compare them to the BL and VN treatments, so copy the equilibrium outcomes from
  # those and relabel them as RS and RS-VN
  
  BL_params = list(p1A = 0.2, deltaA = 0.1,  endowA = 100, endowB = 100)
  CP_params = list(p1A = 0.2, deltaA = 0.1,  endowA = 150, endowB = 50) 
  VN_params = list(p1A = 0.05, deltaA = 0.05, endowA = 100, endowB = 100) 
  CP_VN_params = list(p1A = 0.05, deltaA = 0.05, endowA = 150, endowB = 50)
  RS_params = list(p1A = 0.2, deltaA = 0.1,  endowA = 100, endowB = 100)
  RS_VN_params = list(p1A = 0.05, deltaA = 0.05, endowA = 100, endowB = 100) 
  
  equilibrium.values <- list(BL = BL_params, CP = CP_params, VN = VN_params, CP_VN = CP_VN_params,
                             RS = RS_params, RS_VN = RS_VN_params) %>%
    map(~ append(., list(no.negative = TRUE, p1B = 0.2, deltaB = 0.1, rnds = 8, Cost = 100, pEnd = 0.25))) %>%
    map(~ do.call("cvFunction", .)) %>%
    imap(~ mutate(.x, Name = .y) %>% relocate(Name, 1)) %>%
    bind_rows() %>% as_tibble() %>% mutate(Name = ifelse(Name == "CP_VN", "CP-VN",
                                                         ifelse(Name == "RS_VN", "RS-VN", Name)))
  
  eq.outcomes.full <- equilibrium.values %>% 
    dplyr::select(treatment2 = Name, roundId = Rounds, offer = Val.O, Accept) %>%
    mutate(receiverDecision = ifelse(Accept == TRUE, "ACCEPT", "REJECT"),
           roundId = roundId - 1) %>% select(-Accept)
  
  
  # join equilibrium outcomes with grid for each player-round combination (for consistent formatting
  # with experimental results)
  
  equilibrium.full <- eq.df.full %>% dplyr::left_join(eq.outcomes.full) %>% 
    dplyr::rename(eq_offer = offer, eq_rD =receiverDecision)
  
  # then join these with experimental results, where responsibility treatments
  # are subsetted to cases where both players choose endowments of 100
  
  combined.df.full = experimentalResults %>% 
    dplyr::filter(str_detect(treatment2, "^RS", negate = TRUE)|(i1 == 100 & i2 == 100)) %>% 
    dplyr::mutate(across(where(is.factor), ~ as.character(.))) %>% 
    dplyr::left_join(equilibrium.full) %>% # compare actual to predicted
    dplyr::mutate(offer_diff = offer - eq_offer, 
                  rD_diff = ifelse(receiverDecision == eq_rD, 0, 1))
  
  output <-list(equilibrium.full = equilibrium.full, 
                equilibrium.values = equilibrium.values,
                combined.df.full = combined.df.full) %>% 
    map(as_tibble)
  return(output)
}



